package org.clevernote.main;

import java.util.List;

import org.clevernote.foundations.Authentication;
import org.clevernote.foundations.EvernoteExtension;
import org.clevernote.foundations.StringConstants;
import org.clevernote.foundations.cmd.Cmd;

import ui.cmdline.parser.CommandLineArguments;

import com.evernote.edam.error.EDAMSystemException;
import com.evernote.edam.error.EDAMUserException;
import com.evernote.edam.notestore.NoteFilter;
import com.evernote.edam.notestore.NoteList;
import com.evernote.edam.notestore.NoteStore;
import com.evernote.edam.type.Note;
import com.facebook.thrift.TException;
import com.facebook.thrift.protocol.TBinaryProtocol;
import com.facebook.thrift.transport.THttpClient;

public class CmdOpenNote implements Cmd {

	protected static final int BATCH_SIZE = 100;

	public String getName() {
		return StringConstants.CMD_EDIT_NOTE;
	}

	public void run(CommandLineArguments _cmdline, Authentication _auth)
			throws TException, EDAMSystemException, EDAMUserException {

		THttpClient noteStoreTrans = new THttpClient(_auth.getNoteStoreUrl());
		TBinaryProtocol noteStoreProt = new TBinaryProtocol(noteStoreTrans);
		NoteStore.Client noteStore = new NoteStore.Client(noteStoreProt,
				noteStoreProt);

		String authToken = _auth.getAuthenticationToken();

		final String noteTitle = getNoteName(_cmdline);

		final NoteFilter filter = new NoteFilter();

		EvernoteExtension.NoteVisitor visitor = new EvernoteExtension.NoteVisitor() {
			public void visitNote(Note _note) {
				if (_note.getTitle().contains(noteTitle)) {
					System.out.println(" Going to open " + _note.getTitle());
				}
			}
		};

		EvernoteExtension.findNotes(noteStore, authToken, filter, visitor);

	}

	private String getNoteName(CommandLineArguments _cmdline) {
		String noteName = _cmdline.getString(StringConstants.NOTE, null);

		if (noteName == null) {
			// TODO: use default note name
			return null;
		}

		return noteName;
	}

	/**
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Cmd cmd = new CmdOpenNote();

			CmdRunner.executeCmd(args, cmd);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
